<?php
/**
 * @file album.php
 * @author LJ (liangjian@haibao.com)
 * @date 2015年11月24日 下午7:12:05
 * @brief 相册
 */
namespace haibao\app\data;

class Album extends \haibao\app\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\app\model\data\Album');
    }

    /**
     * 创建相册
     */
    public function createAlbum($model, $type){
        $this->beginTranscation();
        try {
            $this->add($model);
            if($type == \haibao\app\model\data\Album::ALBUM_TYPE_DEFAULT || $type == \haibao\app\model\data\Album::ALBUM_TYPE_COLLECT){
                $this->query('insert into AlbumLog(UserId, albumId, AlbumType, CreateTime) values(%s, %s, %s, %s)', array(
                    $model->UserId, $model->Id, $type, date('Y-m-d H:i:s')
                ));
            }
            if($model->Id && $model->UserId && $type == \haibao\app\model\data\Album::ALBUM_TYPE_COLLECT){
                $this->query('update Collection set AlbumId=%s where UserId=%s', array(
                    $model->Id, $model->UserId
                ));
                \haibao\app\business\Collection::resetImageCount($model->UserId, $model->Id);
            }
            $this->deleteAlbumList($model->UserId);
            $this->deleteAlbumTop3($model->UserId);
        }catch(\Exception $e){
            $this->rollback();
            throw $e;
        }
        $this->endTranscation();
    }
    
    public function getCreateAlbumLog($userId, $type){
        return $this->query('select * from AlbumLog where UserId=%s and AlbumType=%s', array(
            $userId, $type
        ))->fetch_assoc();
    }
    
    public function getAlbumByName($userId, $name){
        $sql = 'select * from Album where UserId=%s and Name=%s and Status=%s';
        return $this->query($sql, array($userId, $name, \haibao\app\model\data\Album::STATUS_ENABLE))->fetch_assoc();
    }

    /**
     * 用户Id和相册是否匹配
     */
    public function pregAlbum($userId, $albumId){
        $sql = 'select * from Album where Id=%s and UserId=%s and Status=%s';
        return $this->query($sql, array($albumId, $userId, \haibao\app\model\data\Album::STATUS_ENABLE))->fetch_assoc();
    }
    
    public function updateAlbumName($userId,$albumId, $name){
        $this->updateById($albumId, array(
            \haibao\app\model\filter\Album::CONDITION_FIELD_NAME_NAME => $name
        ));
        
        $this->deleteAlbumList($userId);
    }
    
    public function getAlbumCount($userId){
        $result = $this->query('select count(*) as count from Album where UserId=%s and Status=%s', array(
            $userId, \haibao\app\model\data\Album::STATUS_ENABLE
        ))->fetch_assoc();
        return intval($result['count']);
    }
    
    public function deleteAlbum($userId, $albumId){
        if(!$userId || !$albumId){
            return false;
        }
        $this->beginTranscation();
        try {
            $this->query('update Album set Status=%s,ImageCount=0 where Id=%s and UserId=%s limit 1', array(
                \haibao\app\model\data\Album::STATUS_DISABLE, $albumId, $userId
            ));
            $this->query('delete from Collection where UserId=%s and AlbumId=%s', array(
                $userId, $albumId
            ));
            $this->deleteAlbumList($userId);
        }catch(\Exception $e){
			$this->rollback();
			throw $e;
		}
        $this->endTranscation();
    }
    
    public function getAlbumByUserId($userId){
        $result = $this->query('select a.Id,a.Name,a.ImageCount,b.AlbumType from `Album` as a left join `AlbumLog` as b on a.Id=b.albumId where a.UserId=%s and a.`Status`=%s order by a.Id asc', array($userId, \haibao\app\model\data\Album::STATUS_ENABLE));
        $retArr = array();
        if($result){
            while ($result->fetch_assoc()) {
                $tmpArr['albumId'] = $result->getData('Id');
                $tmpArr['albumName'] = $result->getData('Name');
                $tmpArr['imageCount'] = $result->getData('ImageCount');
                $tmpArr['albumType'] = $result->getData('AlbumType');
                array_push($retArr, $tmpArr);
            }
        }
        return $retArr;
    }
    
    public function getAlbumList($userId){
        $cache = new \haibao\app\cache\Album();
        $albumList = $cache->getAlbumList($userId);
        if(!$albumList){
            $albumList = $this->getAlbumByUserId($userId);
            $this->setAlbumList($userId, $albumList);
        }
        
        return $albumList;
    }
    
    public function setAlbumList($userId, $data){
        $cache = new \haibao\app\cache\Album();
        return $cache->setAlbumList($userId, $data);
    }
    
    public function deleteAlbumList($userId){
        $cache = new \haibao\app\cache\Album();
        return $cache->deleteAlbumList($userId);
    }
    
    public function getAlbumTop3($userId){
        $cache = new \haibao\app\cache\Album();
        return $cache->getAlbumTop3($userId);
    }
    
    public function setAlbumTop3($userId, $data){
        $cache = new \haibao\app\cache\Album();
        return $cache->setAlbumTop3($userId, $data);
    }
    
    public function deleteAlbumTop3($userId){
        $cache = new \haibao\app\cache\Album();
        return $cache->deleteAlbumTop3($userId);
    }
}